﻿@{
    ViewBag.Title = "采购管理";
    Layout = null;
}
@model TEWorkFlow.Domain.Business.PcPurchaseManage


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>采购管理</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" type="text/css" href="~/Styles/demo.css" />
    <script src="~/scripts/boot.js" type="text/javascript"></script>
</head>
<body>
    <form id="form1" method="post">
    <input name="Id" class="mini-hidden" value="@Model.Id" />
    <div style="padding-left: 11px; padding-bottom: 5px;">
        
<table class="edittable" border="1" cellpadding="0" cellspacing="0">
    <tr>
        <td style="width: 80px;">
            采购单号:
        </td>
        <td style="width: 170px;" colspan="3">
            <input name="PcNumber" value="@Model.Id" class="mini-textbox"  enabled="false"/>
        </td>
        <td style="width: 60px;">
            供货商:
        </td>
        <td style="width: 170px;">
            <input id="EnCode" name="EnCode" value="@Model.EnCode" enabled="false" class="mini-hidden" required="true"/>
            <input name="SupCode" value="@Model.SupCode" text="@Model.SupCode" enabled="false" class="mini-autocomplete" require="true" valueField="SupName" displayField="SupName" url="/Archives/GetAllSupplierArchive" onvaluechanged="onSupCodeValueChanged"  required="true"/>
        </td>
    </tr>
    <tr>
        <td style="width: 60px;">
            门店:
        </td>
        <td style="width: 170px;">
            <input name="bCode" class="mini-combobox"  textField="bName" valueField="Id" emptyText="请选择..." url="/Archives/SearchAllranchesForTree" value="@Model.bCode"  allowInput="false" showNullItem="true" nullItemText="请选择..." required="true"/>
        </td>
        <td style="width: 60px;">
            采购类型:
        </td>
        <td style="width: 170px;">
            <input name="PcType" class="mini-combobox"  textField="text" valueField="id" emptyText="请选择..." url="/Category/PcType" value="@Model.PcType"  allowInput="false" showNullItem="true" nullItemText="请选择..." required="true"/>
        </td>
        <td style="width: 60px;">
            采购日期:
        </td>
        <td style="width: 170px;">
            <input name="PurchaseDate" value="@Model.PurchaseDate" class="mini-datepicker" value="@DateTime.Now.ToString("yyyy-MM-dd")"/>
        </td>
    </tr>
    <tr>
        <td style="width: 60px;">
            是否审核:
        </td>
        <td align="left" style="padding-left: 2px;">
            <div name="IfExamine" class="mini-checkbox" checked="@Model.IfExamine" readOnly="false" text="审核" trueValue="1" falseValue="0">
            </div>
        </td>
        <td style="width: 60px;">
            采购形式:
        </td>
        <td style="width: 170px;">
            <input name="PcForm" value="@Model.PcForm" class="mini-textbox"/>
        </td>
        <td></td>
        <td></td>
    </tr>
</table>
<div id="datagrid1" class="mini-datagrid" style="width: 100%; height: 300px;" idfield="Id"
    emptytext="数据为空，<a href='javascript:newRow()'>增加一条</a>" showemptytext="true"
    showpager="false" ondrawsummarycell="onDrawSummaryCell" showSummaryRow="true">
    <div property="columns">
        <div type="indexcolumn" ></div>
        <div field="GoodsCode" headeralign="center" allowsort="true" displayfield="GoodsName">
            产品 
            <div property="editor" class="mini-combobox" textField="displayText" popupWidth="400"
                valuefield="Id" url="/Archives/GetAllGoodsArchiveAutoComplete/#" onvaluechanged="onValueChanged" allowInput="true">
                <div property="columns">
                    <div header="条形码" field="GoodsBarCode">
                    </div>
                    <div header="拼音码" field="PyCode">
                    </div>
                    <div header="名称" field="GoodsName">
                    </div>
                </div>
            </div>
            @*<input property="editor" class="mini-combobox" style="width:150px;" textField="GoodsName" valueField="Id" showNullItem="true" allowInput="true" onenter="alert('enter')"/>*@
        </div>
        <div field="PurchaseQty" width="30" headeralign="center" align="center" allowsort="true">
            数量
            <input property="editor" class="mini-spinner" style="width: 100%;" required="true"
                minvalue="1" maxvalue="5000" />
        </div>
        <div field="PurchasePrice" width="60" headeralign="center" align="center" allowsort="true"
            datatype="currency" currencyunit="￥">
            单价
        </div>
        <div field="PurchaseMoney" width="60" headeralign="center" align="center" allowsort="true"
            datatype="currency" currencyunit="￥">
            金额
        </div>
        <div field="GoodsBarCode" width="80" headeralign="center" align="center" allowsort="true">
            条码
        </div>
        <div type="checkboxcolumn" field="IsFree" headeralign="center" width="50">
            是否赠品</div>
        <div name="action" width="60" headeralign="center" align="center" renderer="onActionRenderer"
            cellstyle="padding:0;">
            操作</div>
    </div>
</div>

    </div>
    <div style="text-align: center; padding: 0 10px 10px 10px;">
        <a class="mini-button" onclick="onOk" style="width: 60px; margin-right: 20px;">确定</a>&nbsp;
        <a class="mini-button" onclick="onCancel" style="width: 60px;margin-right: 20px;">取消</a>&nbsp;
        <a class="mini-button" onclick="newRow" style="width: 60px;">增加</a>
    </div>
    </form>
    <script type="text/javascript">
        var entityId = "@Model.Id";
        mini.parse();

        var form = new mini.Form("form1");

        function GetData() {
            var o = form.getData();
            return o;
        }
        function CloseWindow(action) {
            if (action == "close" && form.isChanged()) {
                if (confirm("数据被修改了，是否先保存？")) {
                    return false;
                }
            }
            if (window.CloseOwnerWindow) return window.CloseOwnerWindow(action);
            else window.close();
        }
        function onOk(e) {
            SaveData();
        }
        function onCancel(e) {
            CloseWindow("cancel");
        }
        function onDrawSummaryCell(e) {
            var result = e.result;
            var grid = e.sender;
            if (e.field == "GoodsCode") {
                e.cellHtml = "数量："+grid.totalCount;
            }
            if (e.field == "PurchaseQty") {
                try{
                    var sum = 0;
                    for (var i = 0; i < result.data.length; i++) {
                        sum += parseInt(result.data[i].PurchaseQty);
                    }
                    e.cellHtml = "总数：" + sum;
                }
                catch(e){}
            }
            if (e.field == "PurchasePrice") {
                e.cellHtml = "";
            }
            if (e.field == "PurchaseMoney") {
                try {
                    var sum = 0;
                    for (var i = 0; i < result.data.length; i++) {
                        sum += parseInt(result.data[i].PurchaseMoney);
                    }
                    e.cellHtml = "总额：￥" + sum;
                }
                catch (e) { }
            }
        }
    </script>
    
    <script type="text/javascript">
        $(function () {
            $(".mini-textbox").each(function () {
                $(this).css("width", "100%");
            });
            $(".mini-buttonedit").each(function () {
                $(this).css("width", "100%");
            });
        })
        mini.parse();

        var grid = mini.get('datagrid1');
        grid.url = "/Business/GetPurchaseDetailList/" + entityId;
        grid.reload();

        function onActionRenderer(e) {
            var grid = e.sender;
            var record = e.record;
            var uid = record._uid;
            var rowIndex = e.rowIndex;

            var s = ' <a class="Edit_Button" href="javascript:editRow(\'' + uid + '\')" >编辑</a>&nbsp;'
                    + ' <a class="Delete_Button" href="javascript:delRow(\'' + uid + '\')">删除</a>';

            if (grid.isEditingRow(record)) {
                s = '<a class="Update_Button" href="javascript:updateRow(\'' + uid + '\')">保存</a>&nbsp;'
                    + '<a class="Cancel_Button" href="javascript:cancelRow(\'' + uid + '\')">取消</a>'
            }
            return s;
        }
        function newRow() {
            if (grid.isEditing()) {
                if (grid.isEditingRow(grid.getRow(0)) && grid.getRow(0)._state == "added") {
                    return;
                }
                grid.cancelEdit();
            }
            var row = {};
            grid.addRow(row, grid.data.length);

            grid.cancelEdit();
            grid.beginEditRow(row);
            //$(".mini-grid-newRow input.mini-buttonedit-input").eq(0).click();
            setTimeout(function () {
                var num = $(".mini-grid-newRow input.mini-buttonedit-input").eq(0)
                num.focus();
                window.scrollTo(0, 99999);
            }, 200);

        }
        function editRow(row_uid) {
            if (grid.isEditing()) {
                if (grid.isEditingRow(grid.getRow(0)) && grid.getRow(0)._state == "added") {
                    //grid.removeRow(grid.getRow(0));
                    return;
                }
                grid.cancelEdit();
            }
            var row = grid.getRowByUID(row_uid);
            if (row) {
                grid.cancelEdit();
                grid.beginEditRow(row);
            }
        }
        function cancelRow(row_uid) {
            grid.reload();
        }
        function delRow(row_uid) {
            var row = grid.getRowByUID(row_uid);
            if (row) {
                if (confirm("确定删除此记录？")) {
                    grid.loading("删除中，请稍后......");
                    $.ajax({
                        url: "/Business/DeletePurchaseDetail/" + row.Id,
                        success: function (text) {
                            grid.reload();
                        },
                        error: function () {
                        }
                    });
                }
            }
        }

        function updateRow(row_uid, addnew) {
            var row = grid.getRowByUID(row_uid);
            if (grid.isValid() == false) {
                return false;
            }
            grid.commitEdit();
            var rowData = grid.getChanges()[grid.getChanges().length-1];
            if (!rowData) {
                //没有改动
                return;
            }
            if (!rowData.GoodsCode) {
                alert("没有选择产品");
                grid.reload();
                return;
            }
            //grid.loading("保存中，请稍后......");
            var json = mini.encode(rowData);
            newRow();
            $.ajax({
                url: "/Business/SaveGoodsToPurchase/" + entityId,
                data: { "data": json },
                type: "post",
                success: function (text) {
                    //grid.load("", newRow);
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert(jqXHR.responseText);
                }
            });

        }
        var issaving = false;


        function SaveData() {

            var form = new mini.Form("form1");
            var o = form.getData(true);

            form.validate();
            if (form.isValid() == false) return;

            var json = mini.encode([o]);
            issaving = true;
            $.ajax({
                type: "post",
                url: "/Business/SavePurchase",
                data: o,
                cache: false,
                success: function (text) {
                    issaving = false;
                    //CloseWindow("save");
                    if (o.IfExamine == "1") {
                        location.href = "/Query/PurchaseView/" + o.PcNumber;
                    }
                    else {
                        grid.reload();
                    }
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert(jqXHR.responseText + "err");
                    issaving = false;
                    CloseWindow();
                }
            });
        }
        function onSupCodeValueChanged(e) {
            var item = e.selected;
            if (item) {
                mini.get("EnCode").setValue(item.Id);
            }
        }

        function onDCodeButtonEdit(e) {
            var btnEdit = this;
            mini.open({
                url: "/Account/DepartmentSelectTree",
                showMaxButton: false,
                title: "选择部门",
                width: 350,
                height: 350,
                ondestroy: function (action) {
                    if (action == "ok") {
                        var iframe = this.getIFrameEl();
                        var data = iframe.contentWindow.GetData();
                        data = mini.clone(data);
                        if (data) {
                            btnEdit.setValue(data.id);
                            btnEdit.setText(data.id + "(" + data.text + ")");
                        }
                    }
                }
            });
        }

        function onGoodsCodeButtonEdit(e) {
            var btnEdit = this;
            mini.open({
                url: "/Archives/GoodsSelectWindow",
                showMaxButton: false,
                title: "选择商品",
                width: 400,
                height: 350,
                ondestroy: function (action) {
                    if (action == "ok") {
                        var iframe = this.getIFrameEl();
                        var data = iframe.contentWindow.GetData();
                        data = mini.clone(data);
                        if (data) {
                            btnEdit.setValue(data.Id);
                            btnEdit.setText(data.GoodsName);
                        }
                    }
                }
            });
        }
        document.onkeydown = function (e) {
            e = e || window.event;
            if (e.keyCode == 13) {
                //return
                if (grid.isEditing()) {
                    //grid.commitEdit();
                    if (grid.getChanges().length > 0) {//&& grid.getChanges()[0].GoodsCode != undefined
                        updateRow("", true);
                    } else {
                    }
                }
                else {
                    newRow();
                }
                return false;
            }

            //ctrl+s
            if (e.keyCode == 83 && e.ctrlKey) {
                if (issaving == false) {
                    SaveData();
                }
                return false;
            }
            //ecs
            if (e.keyCode == 27) {
                grid.reload();
                return false;
            }
        };
        function onValueChanged(e) {
            var num = $(".mini-grid-newRow input.mini-buttonedit-input").eq(1);
            num.focus();
            $(num).select();
            //document.getElementById(num.prop("id")).setSelectionRange(0, num.val().length);
        }
    </script>

</body>
</html>
